export async function uploadFile(file, fileUploadInfo) {
	try {
		const fileTypeMapping = {
			'audio/mpeg': 'voice_mp3',
			'audio/x-m4a': 'voice_m4a',
			'audio/wav': 'voice_wav',
			'image/jpeg': 'picture_jpg',
			'image/png': 'picture_jpg',
			'video/mp4': 'picture_mp4',
			'text/plain': 'text'
		};

		const correctedFileType = fileTypeMapping[file.type];
		const fileInfo = fileUploadInfo[correctedFileType];
		if (!fileInfo) {
			throw new Error(`未找到文件类型 ${correctedFileType} 的签名信息`);
		}

		const cosHost = /^https?:\/\//.test(fileInfo.cosHost) ? fileInfo.cosHost : 'https://' + fileInfo.cosHost;

		return new Promise((resolve, reject) => {
			const xhr = new XMLHttpRequest();
			const formData = new FormData();

			formData.append('key', fileInfo.cosKey);
			formData.append('policy', fileInfo.policy);
			formData.append('q-ak', fileInfo.qAk);
			formData.append('q-key-time', fileInfo.qKeyTime);
			formData.append('q-signature', fileInfo.qSignature);
			formData.append('q-sign-algorithm', fileInfo.qSignAlgorithm);
			formData.append('file', file);

			xhr.open('POST', cosHost, true);
			xhr.setRequestHeader('Authorization', fileInfo.qSignature);

			xhr.onload = function() {
				if (xhr.status === 204) {
					resolve(xhr.response);
				} else {
					reject('文件上传失败：' + xhr.statusText);
				}
			};

			xhr.onerror = function() {
				reject('文件上传失败：网络错误');
			};

			xhr.send(formData);
		});
	} catch (error) {
		console.error('获取签名信息失败：', error);
		throw error;
	}
}